﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmbeddedSharp.Core {
    public class ExpressionSimplifier : IVisitor {
        #region IVisitor Members

        public object Visit(IdentifierNode node) {
            return node;
        }

        public object Visit(NumberNode node) {
            return node.Value;
        }

        public object Visit(StringNode node) {
            return node;
        }

        public object Visit(EmbeddedSharp.Core.Function.Arithmetics.SubNode node) {
            return this.ProcessChildNodes(node);
        }

        protected object ProcessChildNodes(FuncNode node) {
            foreach (Node child in node.Parameters) {
                object lv = child.Accept(this);
                if (!(lv is NumberNode)) {
                    return node;
                }
            }

            NumberNode nn = new NumberNode();
            nn.Value = Convert.ToString(node.Evaluate());

            return nn;
        }

        public object Visit(EmbeddedSharp.Core.Function.Arithmetics.AddNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Arithmetics.MulNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Arithmetics.ModNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Arithmetics.DivNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Maths.PowerNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Maths.LogNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.LargerThanNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.LessThanNode node) {
            return this.ProcessChildNodes(node);
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.LargerEqualToNode node) {
            throw new NotImplementedException();
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.LessEqualToNode node) {
            throw new NotImplementedException();
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.EqualNode node) {
            throw new NotImplementedException();
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.NotEqualNode node) {
            throw new NotImplementedException();
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.AndNode node) {
            throw new NotImplementedException();
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.OrNode node) {
            throw new NotImplementedException();
        }

        public object Visit(EmbeddedSharp.Core.Function.Logic.NotNode node) {
            throw new NotImplementedException();
        }

        public object Visit(FuncNode node) {
            throw new NotImplementedException();
        }

        #endregion
    }
}
